/**Edit patient presenter**/
// $codepro.audit.disable declareAsInterface, handleNumericParsingErrors, largeNumberOfParameters
package edu.gatech.presenters;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import edu.gatech.datastorage.Gender;
import edu.gatech.datastorage.PatientInformation;
import edu.gatech.userclasses.Nurse;
import edu.gatech.userclasses.Patient;
import edu.gatech.userclasses.User;

/**Edits a patient from a GUI
 * 
 * @author Tyler Natoli
 * @version 1.0
 */
public class EditPatientPresenter {
	
	/**Patient being edited */
	private final Patient currentPatient;
	
	/**Logger*/
	private static final Logger MYLOGGER = Logger.getLogger("My logger");
	
	 /**
	  * Edits patient
	  * @param currentPatient
	  */
	public EditPatientPresenter(Patient currentPatient){
		this.currentPatient = currentPatient;
	}
	
	/**Edits a Patien
	 * 
	 * @param medication
	 * @param allergy
	 * @param name
	 * @param password
	 * @param age
	 * @param gender
	 * @param phoneNumber
	 * @param address
	 * @param pharmacy
	 * @param insurance
	 * @param dateOfBirth
	 */
	public void edit(String medication, String allergy, String name, 
			String password, String age,
			Gender gender, String phoneNumber, String address,
			String pharmacy, String insurance, 
			String dateOfBirth){
		if(isInteger(age)){
			final String[] meds = medication.split(", ");
			final ArrayList<String> medications = new ArrayList<String>();
			for(int i = 0; i < meds.length; i++){
				medications.add(meds[i]);
			}
			final String[] alls = allergy.split(", ");
			final ArrayList<String> allergies = new ArrayList<String>();
			for(int j = 0; j < meds.length; j++){
				allergies.add(alls[j]);
			}
			final PatientInformation info = new PatientInformation(name, password, 
					Integer.parseInt(age), gender, 
							phoneNumber, address, pharmacy, insurance, medications, 
							allergies, dateOfBirth);
			currentPatient.editPatient(info);
		}
		else{
			JOptionPane.showMessageDialog(null, "Invalid age entered");
		}
	}
	
	/**Deletes a patient
	 * 
	 * @param user
	 */
	public void deletePattient(User user){
		final Nurse nurse = (Nurse) user;
		nurse.deletePatient(currentPatient);
		JOptionPane.showMessageDialog(null, "Patient has been Deleted!");
	}
	
	/**
	 * Checks if the age input is an integer
	 * @param input
	 * @return boolean if the int is true
	 */
	public boolean isInteger(String input){
		try{
			Integer.parseInt(input); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			return true;
		}
		catch(NumberFormatException e){
			MYLOGGER.log(Level.SEVERE, "Age not an int", e);
			return false;
		}
	}
	
	/**Overritng toString
	 * @return string
	 */
	public String toString(){
		return "Edit patient panel";
	}
}
